package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.GoodsStore;
import cn.wolfcode.car.business.domain.Store;
import cn.wolfcode.car.business.mapper.GoodsStoreMapper;
import cn.wolfcode.car.business.mapper.StoreMapper;
import cn.wolfcode.car.business.query.StoreQuery;
import cn.wolfcode.car.business.service.IStoreService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StoreServiceImpl implements IStoreService {
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private GoodsStoreMapper goodsStoreMapper;


    @Override
    public TablePageInfo<Store> query(StoreQuery query) {
        //使用paigeHelper插件
        PageHelper.startPage(query.getPageNum(),query.getPageSize());
        //将list集合传入到TablePageInfo进行封装
        return new TablePageInfo<Store>(storeMapper.selectForList(query));
    }



    @Override
    public void save(Store store) {
        storeMapper.insert(store);
    }

    @Override
    public void edit(Store store) {
        storeMapper.updateByPrimaryKey(store);
    }

    @Override
    public Store get(Long id) {
        return storeMapper.selectByPrimaryKey(id);
    }

    @Override
    public void remove(Long id) {
        //查询库存,如果仓库下有库存则仓库不能删除
        List<GoodsStore> list = goodsStoreMapper.checkGoods(id);
        Integer totalAmount =0;
        for (GoodsStore goodsStore : list) {
            totalAmount += goodsStore.getAmounts();
        }
        System.out.println(totalAmount);
        if (totalAmount>0){
            throw new BusinessException("该仓库下还有商品库存数量:"+totalAmount+"!不能删除!");
        }
        /*软删除*/
        storeMapper.updateStatus(id,Store.DELETE_STATUS);
    }

    @Override
    public List<Store> selectAll() {
        return storeMapper.selectAll();
    }
}
